package com.rfsp.mcp.controller.bank;

import com.github.pagehelper.PageInfo;
import com.rfsp.common.base.ResultListVO;
import com.rfsp.common.interceptor.Token;
import com.rfsp.common.log.TrcMsg;
import com.rfsp.common.pojo.po.AreaPO;
import com.rfsp.common.shiro.ShiroUtils;
import com.rfsp.common.util.XssUtils;
import com.rfsp.common.pojo.po.OrgPO;
import com.rfsp.common.pojo.po.UserPO;
import com.rfsp.mcp.enums.McpConst;
import com.rfsp.mcp.pojo.sdto.OrgYbjSDTO;
import com.rfsp.common.pojo.sdto.PageSDTO;
import com.rfsp.mcp.pojo.vo.OrgYbjVO;
import com.rfsp.mcp.service.OrgYbjService;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

@RequiresRoles(value={"mcp_bank"}, logical= Logical.AND)
@Controller("BankHospitalController")
@RequestMapping("/bank/hospital")
public class BankHospitalController extends BankController {

    public Logger logger = LoggerFactory.getLogger(BankHospitalController.class);

    @Autowired
    private OrgYbjService orgYbjService;

    @RequestMapping("/")
    public String index1(@ModelAttribute("dto") OrgYbjSDTO sdto, PageSDTO pageSdto, ModelMap model, BindingResult result ) {
        return index(sdto,pageSdto,model,result);
    }

    //存在三个问题，1、统筹区展示  2、shiro问题  3、邀约按钮，发送短信

    @RequestMapping("/index")
    public String index(@ModelAttribute("dto") OrgYbjSDTO sdto, PageSDTO pageSdto, ModelMap model, BindingResult result ) {
        logger.info(TrcMsg.getMsg(TrcMsg.METHOD_BEGIN, "BaseHospitalController.index"));
    	if(result.hasErrors()) {
			for(ObjectError error : result.getAllErrors ()) {
				System.err.println(error.getDefaultMessage () ) ;
			}
			return "bank/hospital/index";
		}
        OrgPO org = ShiroUtils.getAttribute("org");
        sdto.setBankOrgId(org.getId());
        converDto(sdto);

        XssUtils.copyUnProperties(sdto, sdto);
        PageInfo<OrgYbjVO> pageInfo= orgYbjService.getOrgYbjList(sdto,pageSdto);
        XssUtils.copyProperties(sdto, sdto);
        ResultListVO<OrgYbjVO> vo = ResultListVO.success(pageInfo,sdto);
        model.put("vo", vo);
        logger.info(TrcMsg.getMsg(TrcMsg.METHOD_END, "BaseHospitalController.index"));
        return "bank/hospital/index";
    }

    private void converDto(OrgYbjSDTO sdto) {
        if(sdto != null && StringUtils.isNotEmpty(sdto.getProvince())){
            AreaPO proArea = areaService.getAreaByCode(sdto.getProvince());
            String provinceAndCity =  proArea.getAreaname() ;
            if(StringUtils.isNotEmpty(sdto.getCity())){
                AreaPO cityArea = areaService.getAreaByCode(sdto.getCity());
                provinceAndCity += "-" +cityArea.getAreaname();
            }
            sdto.setProvinceAndCity(provinceAndCity);


        }
        if(sdto !=null && StringUtils.isNotEmpty(sdto.getOrgType())){
            String orgTypeCode = McpConst.YbjOrgTypeDict.getVal(sdto.getOrgType());
            if(StringUtils.isNotEmpty(orgTypeCode)) {
                sdto.setOrgTypeCodeFlag("1"); //1 表示页面传入值有对应的字典code
            }
        }
    }


    /**
     * 关注
     * @param model
     * @param id
     * @return
     */
    @Token
    @GetMapping("/attention")
    public String attention(ModelMap model,Long id) {
        logger.info(TrcMsg.getMsg(TrcMsg.METHOD_BEGIN, "BaseHospitalController.attention"));
        //插入关注表，或者更新关注表
        UserPO user = ShiroUtils.getAttribute("user");
        OrgPO org = ShiroUtils.getAttribute("org");
        Long bankOrgId = org.getId(); //模拟资金方ID=1，后期从shiro中获取
        Long optUserId = user.getId();
        orgYbjService.saveAttention(id,bankOrgId,optUserId);
        logger.info(TrcMsg.getMsg(TrcMsg.METHOD_END, "BaseHospitalController.attention"));
        return alertParentReload(model,"操作成功");
    }

    /**
     * 邀约
     * @param model
     * @param id
     * @return
     */
    @GetMapping("/invite")
    public String invite(ModelMap model,Long id) {
        logger.info(TrcMsg.getMsg(TrcMsg.METHOD_BEGIN, "BaseHospitalController.invite"));
        //判断联系方式
        orgYbjService.sendSms(id);


        logger.info(TrcMsg.getMsg(TrcMsg.METHOD_END, "BaseHospitalController.invite"));
        return alertParentReload(model,"操作成功");
    }
}
